package com.hzau.tourist_service_sys.filter;


import com.alibaba.fastjson.JSONObject;
import com.hzau.tourist_service_sys.result.Result;
import com.hzau.tourist_service_sys.ultils.jjwttool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static java.awt.SystemColor.info;

@Slf4j
@WebFilter(urlPatterns = "/feedback/*")
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest) servletRequest;
        HttpServletResponse rep=(HttpServletResponse) servletResponse;
        String url =req.getRequestURI().toString();
        if (url.contains("fileUpload")){
        filterChain.doFilter(servletRequest,servletResponse);
        return;}
        String jwt=req.getHeader("token");
        if(!StringUtils.hasLength(jwt)){
            Result error=Result.error("not_login");
            String e1=JSONObject.toJSONString(error);
            rep.getWriter().write(e1);
            return;
        }
        if(jjwttool.testJwts(jwt)){
            filterChain.doFilter(servletRequest,servletResponse);
        }
        else {
            Result error=new Result(301,"not_login","null");
            String e1=JSONObject.toJSONString(error);
            rep.getWriter().write(e1);
            return;
        }
    }
}
